package practice_list;

/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-03-27
 * Time:21:25
 */
public class 分隔链表 {
    public ListNode partition(ListNode head, int x) {
        if(head == null){
            return null;
        }
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while(cur != null) {
            if(cur.val < x){
                if(bs == null) {
                    bs = cur;
                    be = cur;
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if(as == null) {
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null){
            return as;

        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }
}
